package CI.aufgabe2.cip.mynodes;

import javax.management.Descriptor;

import CI.aufgabe2.cip.SymbolTable;
import CI.aufgabe2.cip.base.AbstractDescr;
import CI.aufgabe2.cip.descriptor.ArrayDescr;
import CI.aufgabe2.cip.descriptor.IntConstDescr;
import CI.aufgabe2.cip.descriptor.SimpleTypeDescr;

	public class MyArrayTypeNode extends MyAbstractNode {

		private static final long serialVersionUID = 1L;
		
		public final MyAbstractNode indexExpressionNode;
		public final MyAbstractNode type;
		
		public MyArrayTypeNode(MyAbstractNode indexExpressionNode, MyAbstractNode type){
			this.indexExpressionNode=indexExpressionNode;
			this.type=type;
		}
		
		public MyAbstractNode getIndexExpressionNode() {
			return indexExpressionNode;
		}
		
		public MyAbstractNode getType() {
			return type;
		}

		@Override
		public void print() {
			trace("ArrayTypeNode");
			if(indexExpressionNode != null) {
				indexExpressionNode.print();
			}
			if(type != null)
			type.print();
			
			unindent();
		}
		
		
		public AbstractDescr compile(SymbolTable sym) {
			ArrayDescr value = new ArrayDescr(); 
			
			if(indexExpressionNode instanceof MyIntNode)
				value.SetNumberelems(((MyIntNode)indexExpressionNode).getIntVal());
			if(indexExpressionNode instanceof MyIdNode){
				AbstractDescr d = sym.getDescr(((MyIdNode)indexExpressionNode).strval);
				value.SetNumberelems(((IntConstDescr)d).getIntVal());
			}
				
//			value.setSize(value.GetNumberelems());
			
			if(type instanceof MyIdNode){
				AbstractDescr d = null;
				String s = ((MyIdNode) type).strval;
				if(s.equals("integer")){
					d = new SimpleTypeDescr(s);
				}
				else if(s.equals("string")){
					d = new SimpleTypeDescr(s);
				}
				else{
					d = sym.getDescr(s);
				}
				value.SetBasetype(d);
			}else if(type instanceof MyArrayTypeNode || type instanceof MyRecordTypeNode){
				value.SetBasetype(type.compile(sym));
			}
			
			
			return value;
		}
}
